package com.szqhyxj.youxiangju.biz.plan.create;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.plan.PlanModel;
import com.szqhyxj.youxiangju.biz.plan.preview.PlanPreviewActivity;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;

import java.math.BigDecimal;
import java.util.Locale;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PlanCreateActivity extends BaseActivity implements PlanCreateContract.View, NavigationBar.NavigationBarListener {

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.bill_date)
    TextView billDate;
    @BindView(R.id.repay_date)
    TextView repayDate;
    @BindView(R.id.merchant_area)
    TextView merchantArea;
    @BindView(R.id.bill_amount)
    EditText billAmount;


    @BindView(R.id.progress)
    SeekBar progress;
    @BindView(R.id.plan_length_range_start)
    TextView planLengthRangeStart;
    @BindView(R.id.plan_length_range_end)
    TextView planLengthRangeEnd;
    @BindView(R.id.plan_length)
    TextView planLength;
    @BindView(R.id.make)
    Button make;
    @BindView(R.id.ordinary)
    TextView rordinary;
    @BindView(R.id.exquisite)
    TextView rexquisite;
    @BindView(R.id.highquality)
    TextView rhighquality;
    @BindColor(R.color.white)
    int white;
    @BindColor(R.color.invitationColor)
    int invitation;

    private PlanCreateContract.Presenter presenter;
    private String maxAmount;
    public static void start(Activity activity,String maxamount, String bankName, String cardNo, int billDate, int repayDate, String merchantArea) {
        Intent intent = new Intent();
        intent.putExtra(PlanCreateContract.CARD_NAME, bankName);
        intent.putExtra(PlanCreateContract.CARD_NO, cardNo);
        intent.putExtra(PlanCreateContract.BILL_DATE, billDate);
        intent.putExtra(PlanCreateContract.REPAY_DATE, repayDate);
        intent.putExtra(PlanCreateContract.MERCHANT_AREA, merchantArea);
        intent.putExtra(PlanCreateContract.MAXAMOUNT,maxamount);
        intent.setClass(activity, PlanCreateActivity.class);
        activity.startActivity(intent);
    }

    void response(Integer index) {
        String s = billAmount.getText().toString();
        Integer amount;
        if (s.length() == 0) {
            amount = null;
        } else {
            amount = Integer.valueOf(s);
        }
        if (index == null) {
            presenter.responseChange(
                    amount,
                    false,
                    false,
                    false
            );
            return;
        }
        presenter.responseChange(
                amount,
                index == 0,
                index == 1,
                index == 2
        );
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_develop_repayment_plan);
        ButterKnife.bind(this);
        String title = getIntent().getStringExtra(PlanCreateContract.CARD_NAME);
        maxAmount = getIntent().getStringExtra(PlanCreateContract.MAXAMOUNT);
        navigationBar.setNaviTitle(title);
        navigationBar.setNavigationBarListener(this);
        presenter = new PlanCreatePresenter(this);
        presenter.extractIntent(getIntent());
        presenter.setBtName(title);
        progress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                presenter.calculatePlanLength(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        String cardNo = getIntent().getStringExtra(PlanCreateContract.CARD_NO);
        presenter.setIdCard(cardNo);
        response(null);

        billAmount.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                response(null);
            }
        });
    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {

    }

    @OnClick({R.id.ordinary, R.id.exquisite, R.id.highquality})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ordinary:
                response(0);

                break;
            case R.id.exquisite:
                response(1);
                break;
            case R.id.highquality:
                response(2);
                break;
        }
    }

    public abstract static class SimpleTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        presenter = new PlanCreatePresenter(this);
        presenter.extractIntent(intent);
    }

    @Override
    public void showPlanLengthRange(int start, int end) {
        this.planLengthRangeStart.setText(String.format(Locale.getDefault(), "%d天", start));
        this.planLengthRangeEnd.setText(String.format(Locale.getDefault(), "%d天", end));
        this.planLength.setText(String.format(Locale.getDefault(), "%d天", start));
        presenter.setplanLength(start);
    }

    @Override
    public void preview(String planId, PlanModel.PlanCreateEntity createEntity) {
        PlanPreviewActivity.start(this, planId, createEntity);
    }


    @Override
    public void showCardNo(String cardNo) {

    }

    @Override
    public void showBillDate(int billDate) {
        this.billDate.setText(String.format(Locale.getDefault(), "%d日", billDate));
    }

    @Override
    public void showRepayDate(int repayDate) {
        this.repayDate.setText(String.format(Locale.getDefault(), "%d日", repayDate));
    }

    @Override
    public void showMerchantArea(String merchantArea) {
        this.merchantArea.setText(merchantArea);
    }

    @Override
    public void showPlanLength(int planLength) {
        this.planLength.setText(String.format(Locale.getDefault(), "%d天", planLength));
        this.presenter.setplanLength(planLength);
    }

    @Override
    public void enable(int i) {
        if (i == 0) {
            rordinary.setEnabled(true);

        }
        if (i == 1) {

            rexquisite.setEnabled(true);


        }
        if (i == 2) {

            rhighquality.setEnabled(true);
        }

    }

    @Override
    public void disable(int i) {
        if (i == 0) {
            rordinary.setEnabled(false);
            rordinary.setBackground(getResources().getDrawable(R.drawable.background_btn_grey04));
            rordinary.setTextColor(white);
        }
        if (i == 1) {
            rexquisite.setEnabled(false);
            rexquisite.setBackground(getResources().getDrawable(R.drawable.background_btn_grey04));
            rexquisite.setTextColor(white);

        }
        if (i == 2) {
            rhighquality.setEnabled(false);
            rhighquality.setBackground(getResources().getDrawable(R.drawable.background_btn_grey04));
            rhighquality.setTextColor(white);
        }
    }

    @Override
    public void select(int i) {
        if (i == 0) {
            rordinary.setBackground(getResources().getDrawable(R.drawable.background_btn04));
            rordinary.setTextColor(white);

        }
        if (i == 1) {
            rexquisite.setBackground(getResources().getDrawable(R.drawable.background_btn04));
            rexquisite.setTextColor(white);

        }
        if (i == 2) {
            rhighquality.setBackground(getResources().getDrawable(R.drawable.background_btn04));
            rhighquality.setTextColor(white);
        }
    }

    @Override
    public void unSelect(int i) {
        Drawable drawable = getResources().getDrawable(R.drawable.background_shangyibubtn);
        if (i == 0) {
            rordinary.setBackground(drawable);
            rordinary.setTextColor(invitation);
        }
        if (i == 1) {
            rexquisite.setBackground(drawable);
            rexquisite.setTextColor(invitation);

        }
        if (i == 2) {
            rhighquality.setBackground(drawable);
            rhighquality.setTextColor(invitation);
        }
    }

    @Override
    public void disableCreateButton() {
        make.setEnabled(false);
        make.setBackgroundResource(R.drawable.background_btn_grey04);
    }

    @Override
    public void enableCreateButton() {
        make.setEnabled(true);
        make.setBackgroundResource(R.drawable.background_btn04);
    }

    @OnClick(R.id.make)
    public void onClick() {
        if (billAmount.getText().toString().equals("")){
            Toast.makeText(this,"请输入账单金额!",Toast.LENGTH_LONG).show();
        }else{
            BigDecimal amount = new BigDecimal(billAmount.getText().toString());
            BigDecimal maxamount = new BigDecimal(maxAmount);
            if (amount.compareTo(maxamount)==1){
                Toast.makeText(this,"输入金额大于固定额度!",Toast.LENGTH_LONG).show();

            }else{
                presenter.createPlan(billAmount.getText().toString());
            }
        }

    }

}